package com.zmn.oms.admin.controller.conf.cancel.category;

import com.zmn.oms.model.entity.conf.cancel.OrderCancelCategory;
import com.zmn.oms.model.entity.conf.cancel.OrderCancelCategoryQuery;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Optional;

/**
 * @author tanbiao
 * @description ：订单配置取消分类
 * @since 2019/10/21 16:49
 * @modifier
 **/
@Controller
@RequestMapping("/conf/cancel/category/")
public class OrderCancelCategoryListController extends OrderCancelCategoryBaseController {
    private Logger logger = LoggerFactory.getLogger(OrderCancelCategoryListController.class);

    /**
     * 描述: 订单取消分类列表
     *
     * @param query
     *            查询实体
     * @return ModelAndView
     * @author tanbiao
     * @since 2019/10/22
     */
    @RequestMapping(value = "list")
    //@RequiresPermissions("/conf/cancel/cancelCategory/list.action")
    public ModelAndView list(@ModelAttribute OrderCancelCategoryQuery query) {
        ModelAndView mav = new ModelAndView("conf/cancel/category/list");
        int count = orderCancelCategoryService.countByQuery(query);
        query.setTotalCount(count);
        List<OrderCancelCategory> cancelCategories = orderCancelCategoryService.listPageByQuery(query);
        mav.addObject("items", cancelCategories);
        mav.addObject("vo", query);
        return mav;
    }

    /**
     * 功能描述 ：详情页面
     *
     * @param categId
     * @return org.springframework.web.servlet.ModelAndView
     * @author tanbiao
     * @since 2019/10/22
     * @modifier
     */
    @RequestMapping(value = "detail")
    //@RequiresPermissions("/conf/cancel/cancelCategory/detail.action")
    public ModelAndView detail(@RequestParam(value = "categId", defaultValue = "0") Integer categId) {
        ModelAndView mav = new ModelAndView("conf/cancel/category/detail");
        OrderCancelCategory categ = null;
        if (categId == null || categId == 0) {
            categ = new OrderCancelCategory();
        } else {
            categ = Optional.ofNullable(orderCancelCategoryService.findByKey(categId)).orElse(new OrderCancelCategory());
        }
        if (categ.getSort() == null) {
            Integer sort = orderCancelCategoryService.findMaxSort();
            sort = sort == null ? 1 : sort + 1;
            categ.setSort(sort);
        }
        mav.addObject("categ", categ);
        return mav;
    }
}
